<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增菜单')"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>

</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-authority-add">
        <div class="form-group">
            <label class="col-sm-3 control-label ">账号名：</label>
            <div class="col-sm-8">
                <select id="userId" name="userId" class="form-control">
                    <option></option>
                    <option th:each="sysUsers:${sysUsers}" th:value="${sysUsers.userId }"
                            th:text="${sysUsers.userName }"></option>
                </select>
                <input type="checkbox" id="isAdmin">超级管理员<br>

            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">公司：</label>
            <div class="col-sm-8">
                <select id="compID" name="compID" class="form-control" th:onchange="getbumen(this.value)">
                    <option></option>
                    <option th:each="companys:${companys}" th:value="${companys.compID }"
                            th:text="${companys.compAbbr }"></option>
                    <input type="checkbox" id="isManageSubordinate">是否管理下级<br>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">部门：</label>
            <div class="col-sm-8">
                <select id="depId" name="depId" class="form-control select2-multiple" multiple>

                </select>
            </div>
        </div>
    </form>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<script type="text/javascript">
    var prefix = ctx + "sysAuthority";

    function getbumen(value) {
        var html = "";
        $.ajax({
            async: false,
            url: "/empentry/findDep",
            data: {
                "compID": value
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#depId").empty();
                var arry = data;
                html += "<option>"
                    + '所有部门'
                    + "</option>";
                for (var i = 0; i < arry.length; i++) {
                    html += "<option value='"
                        + arry[i].depID
                        + "'>"
                        + arry[i].title
                        + "</option>";
                }
                $("#depId").append(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

                $.modal.alertWarning("错误,请联系管理员,错误信息:" + textStatus);
            }
        });
    }

    function submitHandler() {
        var msg = "";

        var isAdmin = $("input[id='isAdmin']").is(':checked');
        if (isAdmin == true) {
            //设置管理员权限时不能拥有其它权限
            $.ajax({
                async: false,
                url: prefix + "/check",
                data: {"type": "setUpAdmin", "userId": $("#userId").val()},
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data > 0) {
                        msg = "设置管理员时,此用户不能拥有其它权限,请先删除其它权限！";
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.modal.alertWarning("错误,请联系管理员,错误信息:" + textStatus);
                }
            });
        }

        if (msg != "") {
            $.modal.alertWarning(msg);
        } else {
            if ($.validate.form()) {
                var data = $("#form-authority-add").serializeArray();
                var userId = $("#userId").val();
                var compID = $("#compID").val();
                var depId = $("#depId").val();
                var isSelection = $("input[id='isManageSubordinate']").is(':checked');
                var isAdmin = $("input[id='isAdmin']").is(':checked');


                data.push({"name": "userId", "value": userId});
                data.push({"name": "compID", "value": compID});
                data.push({"name": "depIds", "value": depId});
                data.push({"name": "isSelection", "value": isSelection});
                data.push({"name": "isAdmin", "value": isAdmin});
                $.operate.save(prefix + "/add", data);
            }
        }


    }

    $("#isManageSubordinate").change(function () {
        var isSelection = $("input[id='isManageSubordinate']").is(':checked');
        if (isSelection == true) {
            $("#depId").attr("disabled", "disabled");
        } else {
            $("#depId").removeAttr("disabled");
        }
    });

    $("#isAdmin").change(function () {
        var isAdmin = $("input[id='isAdmin']").is(':checked');
        if (isAdmin == true) {
            $("#compID").attr("disabled", "disabled");
            $("#isManageSubordinate").attr("disabled", "disabled");
            $("#depId").attr("disabled", "disabled");
        } else {
            $("#compID").removeAttr("disabled");
            $("#isManageSubordinate").removeAttr("disabled");
            $("#depId").removeAttr("disabled");
        }
    });
</script>
</body>
</html>
